﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Microsoft.Samples.Office.UnifiedCommunications.Gadget.Events;

namespace Microsoft.Samples.Office.UnifiedCommunications.Gadget
{
    public partial class VoiceMailWindow : NavigationWindow
    {
        #region Variable

        private static VoiceMailWindow _instance = null;
        private static int _instanceCount = 0;

        #endregion

        public VoiceMailWindow()
        {
            _instance = this;

            _instanceCount++;

            InitializeCommands();
            InitializeComponent();

            this.MouseDown += new MouseButtonEventHandler(VoiceMailWindow_MouseDown);
            this.Unloaded += new RoutedEventHandler(VoiceMailWindow_Unloaded);
            this.Closing += new System.ComponentModel.CancelEventHandler(VoiceMailWindow_Closing);

            GadgetEventManager.Instance.ApplicationUnloadedEvent += new EventHandler(Instance_ApplicationUnloadedEvent);

            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }

        #region Properties

        public static int InstanceCount { get { return _instanceCount; } }

        public static VoiceMailWindow Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new VoiceMailWindow();
                    _instance.InitializeComponent();
                }
                return _instance;
            }
        }

        #endregion

        #region Loading

        private void NavigationWindow_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeCommands();
        }

        #endregion

        #region Unloading

        void VoiceMailWindow_Unloaded(object sender, RoutedEventArgs e)
        {
            this.MouseDown -= new MouseButtonEventHandler(VoiceMailWindow_MouseDown);
            this.Closing -= new System.ComponentModel.CancelEventHandler(VoiceMailWindow_Closing);

            GadgetEventManager.Instance.ApplicationUnloadedEvent -= new EventHandler(Instance_ApplicationUnloadedEvent);
        }

        private void VoiceMailWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _instanceCount--;
        }

        void Instance_ApplicationUnloadedEvent(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region Other UI Functionality

        void VoiceMailWindow_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                this.DragMove();
            }
            catch
            {

            }
        }

        private void InitializeCommands()
        {
            CommandBindings.Add(new CommandBinding(CloseWindow, CloseWindowExecuted));
            CommandBindings.Add(new CommandBinding(ToggleMinimizedState, MinimizeWindowExecuted));
            CommandBindings.Add(new CommandBinding(ToggleMaximizedState, MaximizeWindowExecuted));
        }

        public static RoutedUICommand CloseWindow = new RoutedUICommand("Close", "CloseWindow", typeof(VoiceMailWindow));

        private void CloseWindowExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            Close();
        }

        public static RoutedUICommand ToggleMaximizedState = new RoutedUICommand("Maximize", "ToggleMaximizedState", typeof(VoiceMailWindow));

        private void MaximizeWindowExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            VoiceMailWindow.Instance.WindowState =
                (VoiceMailWindow.Instance.WindowState == WindowState.Maximized) ? WindowState.Normal : WindowState.Maximized;
        }

        public static RoutedUICommand ToggleMinimizedState = new RoutedUICommand("Minimize", "ToggleMinimizedState", typeof(VoiceMailWindow));

        private void MinimizeWindowExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            VoiceMailWindow.Instance.WindowState =
                (VoiceMailWindow.Instance.WindowState == WindowState.Minimized) ? WindowState.Normal : WindowState.Minimized;
        }

        #endregion
    }
}
